<?php

require_once 'plugins/CategoriesList.php';

class CategoriesListExtended extends CategoriesList {
    
    var $constructor_args = array(
        'date_format'=>'custom date format',
        'limit_to'=>'Run the plugin only for this file',
        // 'post_separator'=>'character(s) to use when joining post titles',
        'unique_title'  => 'Set the page title to this string if not empty'
        );
    
    var $date_format = '%e&nbsp;%B&nbsp;%y';
    var $limit_to = 'list_categories.php';
    // var $post_separator = '&#8214;';
    var $unique_title = 'Categories &raquo; {option_blogname}';
    
    var $default_context = LP_CONTEXT_INDEX;
    var $description = 'Display a list of active categories and their posts titles';
    
    var $nested = true;
    
    var $active = true;
    var $hooks = array('pre_render');
    
    function CategoriesListExtended(&$frontend, $args, $dummy_run=false) {
        $this->CategoriesList($frontend, $args, $dummy_run);

        if (!$dummy_run && !empty($this->limit_to) && basename($_SERVER['PHP_SELF']) != $this->limit_to)
            $this->active = false;
        
    }
    
    function run($hook, &$hook_data) {
        
        $categories =& $this->_getCategories();
        
        if (count($categories) == 0)
            return $this->hide();
        
        $tpl =& $this->_frontend->tpl;
        $db =& $this->_frontend->db;
        
        // $sep = $this->post_separator;
        
        $tpl->setFile('plugin_categorieslistextended', 'plugins/categories_list_extended.xml');
        
        $tpl->setBlock('plugin_categorieslistextended', 'plugin_categorieslistextended_catgroup', 'PLUGIN_CATEGORIESLISTEXTENDED_CATGROUP');
        $tpl->setBlock('plugin_categorieslistextended', 'plugin_categorieslistextended_catgroup_active', 'PLUGIN_CATEGORIESLISTEXTENDED_CATGROUP');
        $tpl->setBlock('plugin_categorieslistextended', 'plugin_categorieslistextended_category', 'PLUGIN_CATEGORIESLISTEXTENDED_CATEGORY');
        $buffer = '';
        
        $q = "select p.id, p.post_name, p.post_title, UNIX_TIMESTAMP(p.post_date) as tstamp, 
            p.post_category as category_id, p2c.category_id as post_category 
            from {$this->_frontend->tables['post2cat']} p2c 
            inner join {$this->_frontend->tables['posts']} p on p.id = p2c.post_id 
            where p.post_status='publish' and p.post_date_gmt <= '{$this->_frontend->where_date}' 
            order by p2c.category_id, p.post_date desc";
        $db->query($q);
        
        $opt_url = $this->_frontend->options['url'];
        $category = null;
        foreach ($db->all() as $post) {
            if ($category != $post['post_category']) {
                if (!is_null($category))
                    $categories[$category]['posts'] = '<li class="first">' . implode("</li><li>", $posts) . '</li>';
                $posts = array();
                $category = $post['post_category'];
            }
            $posts[] = sprintf('<a href="%s/%s">%s</a>',
                               $opt_url,
                               $this->_frontend->getPermalink($post['id'], $post['post_name'], $post['tstamp'], $post['category_id']),
                               htmlspecialchars($post['post_title'], ENT_QUOTES));
        }
        $categories[$category]['posts'] = '<li class="first">' . implode("</li><li>", $posts) . '</li>';

        foreach ($categories as $k=>$cat) {
            if (!$cat['root']) {
                // not a first-level category, continue
                continue;
            }
            $buffer .= $this->_parseCategory($tpl, $cat, $buffer);
        }
        
        $tpl->setVar(array(
            'PLUGIN_CATEGORIESLISTEXTENDED_CATGROUP'    => '',
            'PLUGIN_CATEGORIESLISTEXTENDED_CATEGORY'    => $buffer));
        $tpl->parse('PLUGIN_CATEGORIESLISTEXTENDED', 'plugin_categorieslistextended');
        
        if ($this->unique_title)
            $tpl->setVar('title', $this->unique_title);

    }
    
    function _parseCategory(&$tpl, &$cat) {
        
        if (!isset($cat['posts']))
            $cat['posts'] = '';
        
        if (count($cat['children']) > 0) {
            // parent, start by rendering children
            $buffer = '';
            foreach ($cat['children'] as $k=>$child)
                $buffer .= $this->_parseCategory($tpl, $child);
            
            $tpl->setVar(array(
                'plugin_categorieslistextended_category_children'   => $buffer,
                'plugin_categorieslistextended_category_permalink'  => $cat['category_permalink'],
                'plugin_categorieslistextended_category_name'       => $cat['cat_name'],
                'plugin_categorieslistextended_category_numposts'   => $cat['numposts'],
                'plugin_categorieslistextended_category_posts'      => $cat['posts']));
            if ($cat['numposts'] > 0) {
                // active catgroup
                return $tpl->parse('PLUGIN_CATEGORIESLISTEXTENDED_TMP', 'plugin_categorieslistextended_catgroup_active');
            } else {
                return $tpl->parse('PLUGIN_CATEGORIESLISTEXTENDED_TMP', 'plugin_categorieslistextended_catgroup');
            }
        } else if ($cat['numposts'] > 0) {
            // normal category with posts
            $tpl->setVar(array(
                'plugin_categorieslistextended_category_permalink'  => $cat['category_permalink'],
                'plugin_categorieslistextended_category_name'       => $cat['cat_name'],
                'plugin_categorieslistextended_category_numposts'            => $cat['numposts'],
                'plugin_categorieslistextended_category_posts'      => $cat['posts']));
            return $tpl->parse('PLUGIN_CATEGORIESLISTEXTENDED_TMP', 'plugin_categorieslistextended_category');
        }
        return '';
    }
    
}

?>
